import numpy as np
import math

def distance(p1, p2):
    """计算两点之间的距离"""
    return np.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)

def angle_between_points(p1, p2):
    """计算从点p1到点p2的角度（弧度）"""
    return math.atan2(p2[1] - p1[1], p2[0] - p1[0])

def point_in_circle(point, center, radius):
    """判断点是否在圆内"""
    return distance(point, center) <= radius

def rotate_point(point, center, angle):
    """绕中心点旋转点"""
    x, y = point
    cx, cy = center
    cos_angle = math.cos(angle)
    sin_angle = math.sin(angle)
    
    # 平移到原点
    x -= cx
    y -= cy
    
    # 旋转
    new_x = x * cos_angle - y * sin_angle
    new_y = x * sin_angle + y * cos_angle
    
    # 平移回原位置
    new_x += cx
    new_y += cy
    
    return (new_x, new_y)

def line_intersects_circle(start, end, center, radius):
    """判断线段是否与圆相交"""
    # 将线段表示为 P(t) = start + t * (end - start), t in [0, 1]
    dx = end[0] - start[0]
    dy = end[1] - start[1]
    
    # 圆心到线段起点的向量
    fx = start[0] - center[0]
    fy = start[1] - center[1]
    
    # 二次方程系数 at^2 + bt + c = 0
    a = dx*dx + dy*dy
    b = 2 * (fx*dx + fy*dy)
    c = fx*fx + fy*fy - radius*radius
    
    # 判别式
    discriminant = b*b - 4*a*c
    
    if discriminant < 0:
        # 无实根，不相交
        return False
    
    # 计算根
    sqrt_discriminant = math.sqrt(discriminant)
    t1 = (-b - sqrt_discriminant) / (2*a)
    t2 = (-b + sqrt_discriminant) / (2*a)
    
    # 检查是否有根在[0,1]区间内
    return (0 <= t1 <= 1) or (0 <= t2 <= 1)

def normalize_vector(vector):
    """归一化向量"""
    norm = np.linalg.norm(vector)
    if norm == 0:
        return vector
    return vector / norm